// app.js
App({
    openid: "onCvY5bEHfHILL8FpunCFbOdZgoI",
    globalData: {
        openid: null
    },
    // 定义公共的url
    baseUrl: "https://dasabi999.top/api/",
    // baseUrl: "http:///localhost:7003/",
    openidCallback: null,
    onLaunch() {
        this.login()
    },

    request(params) {
        const _this = this
        // 同时发送异步代码的次数
        let ajaxTimes = 0
        // 同时异步请求数++
        ajaxTimes++;
        // 显示加载中效果
        wx.showLoading({ title: "加载中", mask: true, });
        return new Promise((resolve, reject) => {
            wx.request({
                ...params,
                url: _this.baseUrl + params.url,
                success: (res) => {
                    console.log(res.data);
                    resolve(res.data);
                },
                fail: (err) => {
                    console.log("请求失败 ", err)
                    wx.showModal({
                        title: '请求失败',
                        content: '哎呀~服务器出了点问题呢 ' + err.errMsg,
                        showCancel: false,
                    });
                    reject(err);
                },
                complete: () => {
                    // 同时异步请求数--
                    ajaxTimes--;
                    // 如果同时异步请求数为0
                    if (ajaxTimes === 0) {
                        // 关闭加载中图标
                        wx.hideLoading();
                        wx.hideNavigationBarLoading();
                        wx.stopPullDownRefresh();
                    }
                }
            });

        })
    },

    login() {
        wx.login({
            success: (res) => {
                this.getOpenid(res.code)
            }
        })
    },

    async getOpenid(code) {
        const userInfo = wx.getStorageSync('userInfo')
        this.userInfo = userInfo
        const res = await this.request({
            url: 'front_login', method: "POST", data: {
                code,
                userInfo
            }
        })
        if (res.meta.status >= 300) return wx.showModal({ content: "获取openid失败" + res.msg, showCancel: false })
        this.openid = res.data.openid
        wx.setStorage({ data: this.openid, key: 'openid', })
        if (this.openidCallback) this.openidCallback(res.data.openid)
    },
    showModal(options) {
        return new Promise((resolve, reject) => {
            wx.showModal({
                title: '提示',
                content: '内容',
                showCancel: true,
                confirmText: '知道了',
                confirmColor: '#1B819E',
                ...options,
                success: (result) => {
                    resolve(result)
                },
            });
        })
    },
    showToast(option) {
        wx.showToast({
            title: '提示', icon: 'none', image: '', duration: 1500, mask: false,
            ...option,
        });
    },
    async checkUserStatus() {
        const res1 = await this.request({ url: `getUser`, data: { openid: this.openid }, method: `POST` })
        const user = res1.data[0]
        if (user.status === "待认证") {
            const modal = await this.showModal({ content: `您的用户资料还待认证，请先去完善资料`, confirmText: `马上填写`, })
            if (modal.cancel) return false
            wx.navigateTo({ url: '/pages/mine/userInfo/userInfo' });
            return false
        }
        if (user.status === "待审核") {
            this.showModal({ content: `您的用户资料还待审核，请先耐心等待审核通过才能接单哦`, confirmText: `忍痛等待`, showCancel: false })
            return false
        }
        return true
    }
})
